package com.example.bidding.web;

import com.example.bidding.service.BidService;
import com.example.bidding.service.LoanService;
import com.example.common.Ov.UserOv;
import com.example.common.entity.Const;
import com.example.common.entity.PageParam;
import com.example.common.entity.ResponseCode;
import com.example.common.entity.Rsp;
import com.example.common.pojo.Borrow;
import com.example.common.pojo.Loan;
import com.example.common.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小许
 * @Title:
 * @Package
 * @Description:
 * @date 2020/11/21-15:29
 */
@RestController
@RequestMapping("lend")
public class LendController {
    @Autowired
    private BidService bidService;
    @Autowired
    private LoanService loanService;

    //    获得所有标信息
    @PostMapping("getBid")
    public Rsp getBid(@RequestBody PageParam pageParam) {
        return bidService.getBid(pageParam);
    }

    //    通过id获取标
    @PostMapping("getByPrimaryKey")
    public Rsp getByPrimaryKey(@RequestBody Borrow borrow) {
        return bidService.getByPrimaryKey(borrow.getId());
    }

    //    通过标id获取出借信息 分页
    @PostMapping("getLoanByBorrowId/{id}")
    public Rsp getLoanByBorrowId(@PathVariable("id") String id, @RequestBody PageParam pageParam) {
        return loanService.getLoanByBorrowId(id, pageParam);
    }

    //通过标id获取出借信息 不分页
    @RequestMapping("getPaiedLoanByBorrowId")
    public List<Loan> getPaiedLoanByBorrowId(@RequestParam Integer borrowId) {
        return loanService.getPaiedLoanByBorrowId(borrowId);
    }


    //    添加出借信息,下单
    @PostMapping("addLoan")
    public Rsp addLoan(@RequestHeader("Authorization") String token, @RequestBody Loan loan) {
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        if (StringUtils.isEmpty(user)) {
            return new Rsp(ResponseCode.ERROR, "请先登录");
        }
        loan.setUserId(user.getId());
        Rsp rsp = null;
        try {
            rsp = loanService.addLoan(loan);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return rsp;
    }

    //关闭出借订单
    @RequestMapping("closeLoan")
    public void closeLoan(@RequestParam String orderNo) {
        loanService.closeLoan(orderNo);
    }

    //支付订单
    @PostMapping("pay")
    public Rsp pay(@RequestHeader("Authorization") String token,@RequestBody Loan loan) {
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        if (StringUtils.isEmpty(userOv)) {
            return new Rsp(ResponseCode.ERROR, "请先登录");
        }
        return loanService.pay(userOv, loan);
    }

    //获取投标的的金额和是否进行了投标
    @GetMapping("getLoanMoney")
    public Rsp getLoanMoney(@RequestHeader("Authorization") String token) {
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        return loanService.getLoanMoney(userOv);
    }

    //获取收益信息
    @GetMapping("getProfitInfo")
    public Rsp getProfitInfo(@RequestHeader("Authorization") String token) {
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        return loanService.getProfitInfo(userOv);
    }

    //获取出借信息
    @PostMapping("getLoanInfo")
    public Rsp getLoanInfo(@RequestHeader("Authorization") String token, @RequestBody PageParam pageParam) {
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        return loanService.getLoanInfo(userOv,pageParam);
    }
}
